package com.zmc.admin.infrastructure.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zmc.admin.infrastructure.constant.UserConstants;
import com.zmc.admin.infrastructure.bean.query.SysPostQuery;
import com.zmc.common.utils.PageUtils;
import com.zmc.common.utils.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.zmc.admin.infrastructure.dao.SysPostDao;
import com.zmc.admin.infrastructure.entity.SysPostEntity;
import com.zmc.admin.infrastructure.service.SysPostService;


@Service("sysPostService")
public class SysPostServiceImpl extends ServiceImpl<SysPostDao, SysPostEntity> implements SysPostService {


    @Override
    public PageUtils pagePostList(SysPostQuery query) {
        Page<SysPostEntity> page = baseMapper.selectPage(new Page<>(query.getPageNum(), query.getPageSize()), getWrapper(query));
        return new PageUtils(page);
    }

    @Override
    public List<SysPostEntity> selectPostList(SysPostQuery query) {
        return baseMapper.selectList(getWrapper(query));
    }

    @Override
    public boolean checkPostNameUnique(SysPostEntity post) {
        Long postId = StringUtils.isNull(post.getPostId()) ? -1L : post.getPostId();
        SysPostQuery query = new SysPostQuery();
        query.setPostName(post.getPostName());
        SysPostEntity info = getOne(getWrapper(query));
        if (StringUtils.isNotNull(info) && info.getPostId().longValue() != postId.longValue()) {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    @Override
    public boolean checkPostCodeUnique(SysPostEntity post) {
        long postId = StringUtils.isNull(post.getPostId()) ? -1L : post.getPostId();
        SysPostQuery query = new SysPostQuery();
        query.setPostName(post.getPostCode());
        SysPostEntity info = getOne(getWrapper(query));
        if (StringUtils.isNotNull(info) && info.getPostId() != postId) {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    @Override
    public List<SysPostEntity> selectPostsByUserName(String userName) {
        return baseMapper.selectPostsByUserName(userName);
    }

    @Override
    public List<Long> selectPostListByUserId(Long userId) {
        return baseMapper.selectPostListByUserId(userId);
    }

    private LambdaQueryWrapper<SysPostEntity> getWrapper(SysPostQuery query) {
        LambdaQueryWrapper<SysPostEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(StringUtils.isNotEmpty(query.getPostCode()), SysPostEntity::getPostCode, query.getPostCode())
                .eq(StringUtils.isNotEmpty(query.getPostName()), SysPostEntity::getPostName, query.getPostName())
                .eq(StringUtils.isNotEmpty(query.getStatus()), SysPostEntity::getStatus, query.getStatus());
        return wrapper;
    }
}